# -*- coding: utf-8 -*-
import maya.cmds as cmds
import maya.mel as mel
import os
import datetime
import random
import traceback
import os
import re
class Check_Namespace ():
	def Check_Namespace(self):
		log = ''
		''' 未知错误 后续检查 启动后 cmds.referenceQuery(Ref,referenceNode=True)  查询不到名字
		re_list = cmds.ls (references = 1)
		for tmp in re_list :
			connections = cmds.listConnections (tmp, s=True, d=False)
			
			# 没有连接的引用节点可以删除
			cmds.lockNode (tmp, lock=False)
			cmds.delete (tmp)
		'''
		namespaces = cmds.namespaceInfo (":" , listOnlyNamespaces = True , recurse = True , fn = 1)
		namespaces.reverse ()
		getNamespaces = [ns for ns in namespaces if ns not in ("UI" , "shared")]
		for namespace in getNamespaces :
			res = []
			namespace_infos = cmds.namespaceInfo (namespace , listNamespace = True , recurse = True , fn = 1)
			if namespace_infos is None :
				continue
			for obj in namespace_infos :
				for ob in cmds.ls (obj , ap = 1) :
					if cmds.objectType (ob) not in ["mesh" , "nurbsCurve" ,"nurbsSurface", "gpuCache"] :
						res.append (True)
					else :
						res.append (False)
			if all (res) :
				cmds.namespace (removeNamespace = namespace , mergeNamespaceWithParent = True)

		re_list = cmds.ls (references=1)
		for tmp in re_list :
			if cmds.referenceQuery (tmp , isLoaded = 1) :
				RfMaPath = cmds.referenceQuery (tmp , filename = 1)
				RfNameSpace = RfMaPath.split ('/')
				nameSpace_split = RfNameSpace[-1].split ('.ma')
				RfNameSpace = nameSpace_split[1].strip ('{}')
				art = nameSpace_split[0] + RfNameSpace
				getNameSpace = cmds.file (RfMaPath , q = 1 , ns = 1)

				temp = "temp_" + str (random.randint (1000 , 9999))
				cmds.namespace (f = 1 , rename = [getNameSpace , temp])
				if cmds.namespace (ex = art) :
					LeavingNsObj = cmds.ls (art + ':*')
					if len (LeavingNsObj) :
						for LeavingNsObjtmp in LeavingNsObj :
							if getNameSpace != art :
								cmds.lockNode (LeavingNsObjtmp , lock = 0)
								cmds.delete (LeavingNsObjtmp)
								#cmds.namespace (f = 1 , rename = [getNameSpace , temp])
								#cmds.namespace (art , removeNamespace = 1)


		print("--------------------------------------------------------------------")
		re_list = cmds.ls (references = 1)
		for tmp in re_list :
			if cmds.referenceQuery (tmp , isLoaded = 1) :
				RfMaPath = cmds.referenceQuery (tmp , filename = 1)
				RfNameSpace = RfMaPath.split ('/')
				nameSpace_split = RfNameSpace[-1].split ('.ma')
				RfNameSpace = nameSpace_split[1].strip ('{}')
				art = nameSpace_split[0] + RfNameSpace
				getNameSpace = cmds.file (RfMaPath , q = 1 , ns = 1)
				cmds.namespace (f = 1 , rename = [getNameSpace , art])
				getRefNewNameSpace = cmds.file (RfMaPath , q = 1 , ns = 1)
				log += getRefNewNameSpace + "-YES,"
		print ("////////////////////////////////////////////////////////////////")
		return (log)
		'''
				if cmds.namespace (ex = art) :
					LeavingNsObj = cmds.ls (art + ':*')
					if len (LeavingNsObj) :
						for LeavingNsObjtmp in LeavingNsObj :
							if getNameSpace != art:
								cmds.lockNode (LeavingNsObjtmp , lock = 0)
								cmds.delete (LeavingNsObjtmp)
								cmds.namespace (art , removeNamespace = 1)
				temp = "temp_" + str(random.randint(1000, 9999))
				cmds.namespace (f = 1 , rename = [getNameSpace , temp])
				cmds.namespace (f = 1 , rename = [temp , art])
				getRefNewNameSpace = cmds.file (RfMaPath , q = 1 , ns = 1)
				log += getRefNewNameSpace + "-YES,"
		return (log)
		'''